package com.tj.api.task;

import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.common.conf.constant.RedisKey;
import com.tj.bo.common.conf.util.IpUtils;
import com.tj.bo.product.client.ProductService;
import com.tj.bo.product.client.vo.Product;
import com.tj.bo.user.client.UserProductService;
import com.tj.bo.user.server.dao.UserProductOddsDao;
import com.tj.bo.user.server.entity.bussiness.UserHaveOddsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @program: binary-option
 * @description 回滚大盘收益率
 * @author: binary
 * @create: 2020-08-07 17:05
 **/
@Component
public class RollbackMarketOddsTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(RollbackMarketOddsTask.class);

    @Autowired
    private RedisConfigUtil redisDbConfigUtil;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private UserProductOddsDao userProductOddsDao;
    @Autowired
    private RedisClient redisClient;

//    @Scheduled(cron = "0 0 2 */1 * ?")
    public void rollbackMarketOddsTask() {

        String ip = IpUtils.getIp();
        String taskIp = redisDbConfigUtil.getTaskIp();
        if (!taskIp.equalsIgnoreCase(ip)) {
            LOGGER.info("============ rollbackMarketOddsTask IP不匹配不执行任务 taskIp:{}, localIp:{}  ==========", taskIp, ip);
            return;
        }
        List<Product> products = productService.getAllProductList();
        for (Product product : products) {
            productService.updateProductByOdds(product.getProductCode());
            LOGGER.info("============ 回滚大盘收益率任务 productCode:{}, odds:{} ===========", product.getProductCode(), product.getOdds());
        }

        Page page = new Page(20000);
        List<UserHaveOddsEntity> userList = userProductOddsDao.selectUserHaveOddsList(new Date(), page);
        if (CollectionUtils.isNotEmpty(userList)) {
            for (int i = 0; i < userList.size(); i++) {
                UserHaveOddsEntity userHaveOddsEntity = userList.get(i);
                if (StringUtils.isEmpty(userHaveOddsEntity.getUserCode()) || StringUtils.isEmpty(userHaveOddsEntity.getProductCode())) {
                    continue;
                }
                //写入odd处理队列 单线程job处理可以避免并发问题
                redisClient.listLeftPush(RedisKey.BINARY_OPTION_ODDS_CALC_LIST, userHaveOddsEntity.getUserCode() + "," + userHaveOddsEntity.getProductCode());
                LOGGER.info("用户规则触发写入队列,{},userCode:{},productCode:{}", i, userHaveOddsEntity.getUserCode(), userHaveOddsEntity.getProductCode());
            }
        }
        LOGGER.info("用户规则处理完成,userList:{}", userList.size());
    }

}
